import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post } from '@nestjs/common';
import { EntityManager, Transaction, TransactionManager } from 'typeorm';
import { CreateRoleDto } from './role.dto';
import { RoleService } from './role.service';
import { Role } from './role.entity';

@Controller('role')
export class RoleController {
    // 使用方式需要使用@Inject的方式
    constructor (
      private readonly roleService : RoleService,
    ) { }

    @Post()
    @Transaction()
    async create(@Body() data: CreateRoleDto, @TransactionManager() manager: EntityManager,) {
        return this.roleService.create(data, manager);
    }

    @Get()
    async list(): Promise<Role[]> {
        return await this.roleService.list();
    }

    @Delete(':id')
    async del(@Param('id', new ParseIntPipe()) id: number) {
        return await this.roleService.delete(id);
    }
}
